import useGlobalStore from "@/stores/global.store";
import { storeToRefs } from "pinia";
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
// import Layout from "../views/Layout/Layout.vue";
import menuRoutes from "./menuRoutes";
import whiteRoutes from "./whiteRoutes";
import regRoutes from "./regRoutes";
const isDynamicRoutes = false;
const routes: Array<RouteRecordRaw> = isDynamicRoutes
    ? [...whiteRoutes]
    : [
          {
              path: "/",
              component: () => import("../views/Layout/Layout.vue"),
              // component: Layout,
              children: menuRoutes,
          },
          ...whiteRoutes,
          ...regRoutes,
      ];

// const routes: Array<RouteRecordRaw> = [
//     {
//         path: "/login",
//         component: () => import("../views/Login/Login.vue"),
//     },
//     {
//         path: "/",
//         component: () => import("../views/Layout/Layout.vue"),
//         // component: Layout,
//         children: menuRoutes,
//     },
// ];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
});

// router.beforeEach(async (to, form) => {
//     const { userInfo, token, menuRoutes, authRoutes } = storeToRefs(
//         useGlobalStore(),
//     );
//     const { getMenu, generateRoutes } = useGlobalStore();

//     if (!token?.value && to.path !== "/login") {
//         return { path: "/login", replace: true };
//     }

//     if (token.value && to.path == "/login") {
//         return { path: "/", replace: true };
//     }

//     if (!menuRoutes.value && to.path != "/login") {
//         await getMenu();

//         function addDynamicRoutes(routes: any[]) {
//             console.log(
//                 "dynamicRoutes",
//                 isDynamicRoutes,
//                 authRoutes.value,
//                 routes,
//             );
//             if (isDynamicRoutes) {
//                 // 动态添加路由
//                 if (!authRoutes.value) {
//                     let authRoutes: any[] = generateRoutes(
//                         menuRoutes.value ? [...menuRoutes.value] : [],
//                     );

//                     const LayoutRoute = {
//                         path: "/",
//                         component: () => import("../views/Layout/Layout.vue"),
//                         children: authRoutes || [],
//                     };

//                     router.addRoute(LayoutRoute);

//                     regRoutes.forEach((route) => {
//                         router.addRoute(route);
//                     });

//                     if (to.path == "/workbench" && token.value) {
//                         if (
//                             authRoutes.findIndex(
//                                 (item) => item.path == "workbench",
//                             ) != -1
//                         ) {
//                             return { path: "/workbench" };
//                         } else {
//                             return { path: "/" };
//                         }
//                     }
//                     // console.log(to, router);
//                     return { ...to, replace: true };
//                 }
//             }
//         }

//         return addDynamicRoutes(menuRoutes.value ? [...menuRoutes.value] : []);
//     }
// });

export default router;
